package cn.hlife.com.hlfhaemocyte.systemsetting.view;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.hlife.com.hlfhaemocyte.MainActivity;
import cn.hlife.com.hlfhaemocyte.R;
import cn.hlife.com.hlfhaemocyte.utils.FragmentUtils;
import cn.hlife.com.hlfhaemocyte.utils.SharedPreferencesUtils;

/**
 * Created by Administrator on 2017/7/27.
 */

public class CharSetting extends Fragment implements View.OnClickListener{


    @BindView(R.id.RN1)
    EditText RN1;
    @BindView(R.id.RN2)
    EditText RN2;
    @BindView(R.id.RN3)
    EditText RN3;
    @BindView(R.id.RN4)
    EditText RN4;
    @BindView(R.id.RN5)
    EditText RN5;
    @BindView(R.id.RN6)
    EditText RN6;
    @BindView(R.id.RN7)
    EditText RN7;
    @BindView(R.id.RN8)
    EditText RN8;
    @BindView(R.id.RN9)
    EditText RN9;
    @BindView(R.id.RN10)
    EditText RN10;
    @BindView(R.id.RN11)
    EditText RN11;
    @BindView(R.id.RN12)
    EditText RN12;
    @BindView(R.id.RN13)
    EditText RN13;
    @BindView(R.id.RN14)
    EditText RN14;
    @BindView(R.id.RN15)
    EditText RN15;
    @BindView(R.id.RN16)
    EditText RN16;
    @BindView(R.id.RN17)
    EditText RN17;
    @BindView(R.id.RN18)
    EditText RN18;
    @BindView(R.id.RN19)
    EditText RN19;
    @BindView(R.id.RN20)
    EditText RN20;
    @BindView(R.id.WN1)
    EditText WN1;
    @BindView(R.id.WN2)
    EditText WN2;
    @BindView(R.id.WN3)
    EditText WN3;
    @BindView(R.id.WN4)
    EditText WN4;
    @BindView(R.id.WN5)
    EditText WN5;
    @BindView(R.id.WN6)
    EditText WN6;
    @BindView(R.id.WN7)
    EditText WN7;
    @BindView(R.id.WN8)
    EditText WN8;
    @BindView(R.id.WN9)
    EditText WN9;
    @BindView(R.id.WN10)
    EditText WN10;
    @BindView(R.id.WN11)
    EditText WN11;
    @BindView(R.id.WN12)
    EditText WN12;
    @BindView(R.id.WN13)
    EditText WN13;
    @BindView(R.id.WN14)
    EditText WN14;
    @BindView(R.id.WN15)
    EditText WN15;
    @BindView(R.id.WN16)
    EditText WN16;
    @BindView(R.id.WN17)
    EditText WN17;
    @BindView(R.id.WN18)
    EditText WN18;
    @BindView(R.id.WN19)
    EditText WN19;
    @BindView(R.id.WN20)
    EditText WN20;
    @BindView(R.id.PN1)
    EditText PN1;
    @BindView(R.id.PN2)
    EditText PN2;
    @BindView(R.id.PN3)
    EditText PN3;
    @BindView(R.id.PN4)
    EditText PN4;
    @BindView(R.id.PN5)
    EditText PN5;
    @BindView(R.id.PN6)
    EditText PN6;
    @BindView(R.id.PN7)
    EditText PN7;
    @BindView(R.id.PN8)
    EditText PN8;
    @BindView(R.id.PN9)
    EditText PN9;
    @BindView(R.id.PN10)
    EditText PN10;
    @BindView(R.id.PN11)
    EditText PN11;
    @BindView(R.id.PN12)
    EditText PN12;
    @BindView(R.id.PN13)
    EditText PN13;
    @BindView(R.id.PN14)
    EditText PN14;
    @BindView(R.id.PN15)
    EditText PN15;
    @BindView(R.id.PN16)
    EditText PN16;
    @BindView(R.id.PN17)
    EditText PN17;
    @BindView(R.id.PN18)
    EditText PN18;
    @BindView(R.id.PN19)
    EditText PN19;
    @BindView(R.id.PN20)
    EditText PN20;
    Unbinder unbinder;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.char_setting, container, false);
        unbinder = ButterKnife.bind(this, view);
        ((MainActivity) getActivity()).middleText.setText("直方图设置");
        ((MainActivity) getActivity()).rightText.setOnClickListener(this);
        ((MainActivity) getActivity()).rightText.setText("保存");
        ((MainActivity) getActivity()).rightText.setVisibility(View.VISIBLE);
        ((MainActivity) getActivity()).returnImageView.setVisibility(View.VISIBLE);
        ((MainActivity) getActivity()).returnImageView.setOnClickListener(this);
        initView();
        return view;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if(!hidden){
            ((MainActivity) getActivity()).rightText.setOnClickListener(this);
            ((MainActivity) getActivity()).rightText.setText("保存");
            ((MainActivity) getActivity()).rightText.setVisibility(View.VISIBLE);
            ((MainActivity) getActivity()).returnImageView.setVisibility(View.VISIBLE);
            ((MainActivity) getActivity()).returnImageView.setOnClickListener(this);
            initView();
        }
    }

    public void  initView(){
        String jsonData=  SharedPreferencesUtils.getInstance().getChartParameter(getContext());
            try {
                JSONObject   map=new JSONObject(jsonData);
                RN1.setText(TextUtils.isEmpty(map.getString("RN1"))?"1":map.getString("RN1"));
                RN2.setText(TextUtils.isEmpty(map.getString("RN2"))?"1":map.getString("RN2"));
                RN3.setText(TextUtils.isEmpty(map.getString("RN3"))?"1":map.getString("RN3"));
                RN4.setText(TextUtils.isEmpty(map.getString("RN4"))?"1":map.getString("RN4"));
                RN5.setText(TextUtils.isEmpty(map.getString("RN5"))?"1":map.getString("RN5"));
                RN6.setText(TextUtils.isEmpty(map.getString("RN6"))?"1":map.getString("RN6"));
                RN7.setText(TextUtils.isEmpty(map.getString("RN7"))?"1":map.getString("RN7"));
                RN8.setText(TextUtils.isEmpty(map.getString("RN8"))?"1":map.getString("RN8"));
                RN9.setText(TextUtils.isEmpty(map.getString("RN9"))?"1":map.getString("RN9"));
                RN10.setText(TextUtils.isEmpty(map.getString("RN10"))?"1":map.getString("RN10"));
                RN11.setText(TextUtils.isEmpty(map.getString("RN11"))?"1":map.getString("RN11"));
                RN12.setText(TextUtils.isEmpty(map.getString("RN12"))?"1":map.getString("RN12"));
                RN13.setText(TextUtils.isEmpty(map.getString("RN13"))?"1":map.getString("RN13"));
                RN14.setText(TextUtils.isEmpty(map.getString("RN14"))?"1":map.getString("RN14"));
                RN15.setText(TextUtils.isEmpty(map.getString("RN15"))?"1":map.getString("RN15"));
                RN16.setText(TextUtils.isEmpty(map.getString("RN16"))?"1":map.getString("RN16"));
                RN17.setText(TextUtils.isEmpty(map.getString("RN17"))?"1":map.getString("RN17"));
                RN18.setText(TextUtils.isEmpty(map.getString("RN18"))?"1":map.getString("RN18"));
                RN19.setText(TextUtils.isEmpty(map.getString("RN19"))?"1":map.getString("RN19"));
                RN20.setText(TextUtils.isEmpty(map.getString("RN20"))?"1":map.getString("RN20"));
                WN1.setText(TextUtils.isEmpty(map.getString("WN1"))?"1":map.getString("WN1"));
                WN2.setText(TextUtils.isEmpty(map.getString("WN2"))?"1":map.getString("WN2"));
                WN3.setText(TextUtils.isEmpty(map.getString("WN3"))?"1":map.getString("WN3"));
                WN4.setText(TextUtils.isEmpty(map.getString("WN4"))?"1":map.getString("WN4"));
                WN5.setText(TextUtils.isEmpty(map.getString("WN5"))?"1":map.getString("WN5"));
                WN6.setText(TextUtils.isEmpty(map.getString("WN6"))?"1":map.getString("WN6"));
                WN7.setText(TextUtils.isEmpty(map.getString("WN7"))?"1":map.getString("WN7"));
                WN8.setText(TextUtils.isEmpty(map.getString("WN8"))?"1":map.getString("WN8"));
                WN9.setText(TextUtils.isEmpty(map.getString("WN9"))?"1":map.getString("WN9"));
                WN10.setText(TextUtils.isEmpty(map.getString("WN10"))?"1":map.getString("WN10"));
                WN11.setText(TextUtils.isEmpty(map.getString("WN11"))?"1":map.getString("WN11"));
                WN12.setText(TextUtils.isEmpty(map.getString("WN12"))?"1":map.getString("WN12"));
                WN13.setText(TextUtils.isEmpty(map.getString("WN13"))?"1":map.getString("WN13"));
                WN14.setText(TextUtils.isEmpty(map.getString("WN14"))?"1":map.getString("WN14"));
                WN15.setText(TextUtils.isEmpty(map.getString("WN15"))?"1":map.getString("WN15"));
                WN16.setText(TextUtils.isEmpty(map.getString("WN16"))?"1":map.getString("WN16"));
                WN17.setText(TextUtils.isEmpty(map.getString("WN17"))?"1":map.getString("WN17"));
                WN18.setText(TextUtils.isEmpty(map.getString("WN18"))?"1":map.getString("WN18"));
                WN19.setText(TextUtils.isEmpty(map.getString("WN19"))?"1":map.getString("WN19"));
                WN20.setText(TextUtils.isEmpty(map.getString("WN20"))?"1":map.getString("WN20"));
                WN20.setText(TextUtils.isEmpty(map.getString("WN20"))?"1":map.getString("WN20"));
                PN1.setText(TextUtils.isEmpty(map.getString("PN1"))?"1":map.getString("PN1"));
                PN2.setText(TextUtils.isEmpty(map.getString("PN2"))?"1":map.getString("PN2"));
                PN3.setText(TextUtils.isEmpty(map.getString("PN3"))?"1":map.getString("PN3"));
                PN4.setText(TextUtils.isEmpty(map.getString("PN4"))?"1":map.getString("PN4"));
                PN5.setText(TextUtils.isEmpty(map.getString("PN5"))?"1":map.getString("PN5"));
                PN6.setText(TextUtils.isEmpty(map.getString("PN6"))?"1":map.getString("PN6"));
                PN7.setText(TextUtils.isEmpty(map.getString("PN7"))?"1":map.getString("PN7"));
                PN8.setText(TextUtils.isEmpty(map.getString("PN8"))?"1":map.getString("PN8"));
                PN9.setText(TextUtils.isEmpty(map.getString("PN9"))?"1":map.getString("PN9"));
                PN10.setText(TextUtils.isEmpty(map.getString("PN10"))?"1":map.getString("PN10"));
                PN11.setText(TextUtils.isEmpty(map.getString("PN11"))?"1":map.getString("PN11"));
                PN12.setText(TextUtils.isEmpty(map.getString("PN12"))?"1":map.getString("PN12"));
                PN13.setText(TextUtils.isEmpty(map.getString("PN13"))?"1":map.getString("PN13"));
                PN14.setText(TextUtils.isEmpty(map.getString("PN14"))?"1":map.getString("PN14"));
                PN15.setText(TextUtils.isEmpty(map.getString("PN15"))?"1":map.getString("PN15"));
                PN16.setText(TextUtils.isEmpty(map.getString("PN16"))?"1":map.getString("PN16"));
                PN17.setText(TextUtils.isEmpty(map.getString("PN17"))?"1":map.getString("PN17"));
                PN18.setText(TextUtils.isEmpty(map.getString("PN18"))?"1":map.getString("PN18"));
                PN19.setText(TextUtils.isEmpty(map.getString("PN19"))?"1":map.getString("PN19"));
                PN20.setText(TextUtils.isEmpty(map.getString("PN20"))?"1":map.getString("PN20"));
            } catch (JSONException e) {
                e.printStackTrace();
            }
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onClick(View v) {
       int  viewID= v.getId();
        switch (viewID){
            case  R.id.left_text:
                save();
                break;
            case  R.id.return_image:
                ((MainActivity)getActivity()).currentFragment = FragmentUtils.switchFragment
                        (getFragmentManager(), R.id.fragment_content, ((MainActivity)getActivity()).currentFragment, SettingFragment.class, null);
                break;
        }

    }

    public  void  save(){
       JSONObject map=new JSONObject();
        try {
            map.put("RN1",RN1.getText().toString());
            map.put("RN2",RN2.getText().toString());
            map.put("RN3",RN3.getText().toString());
            map.put("RN4",RN4.getText().toString());
            map.put("RN5",RN5.getText().toString());
            map.put("RN6",RN6.getText().toString());
            map.put("RN7",RN7.getText().toString());
            map.put("RN8",RN8.getText().toString());
            map.put("RN9",RN9.getText().toString());
            map.put("RN10",RN10.getText().toString());
            map.put("RN11",RN11.getText().toString());
            map.put("RN12",RN12.getText().toString());
            map.put("RN13",RN13.getText().toString());
            map.put("RN14",RN14.getText().toString());
            map.put("RN15",RN15.getText().toString());
            map.put("RN16",RN16.getText().toString());
            map.put("RN17",RN17.getText().toString());
            map.put("RN18",RN18.getText().toString());
            map.put("RN19",RN19.getText().toString());
            map.put("RN20",RN20.getText().toString());
            map.put("WN1",WN1.getText().toString());
            map.put("WN2",WN2.getText().toString());
            map.put("WN3",WN3.getText().toString());
            map.put("WN4",WN4.getText().toString());
            map.put("WN5",WN5.getText().toString());
            map.put("WN6",WN6.getText().toString());
            map.put("WN7",WN7.getText().toString());
            map.put("WN8",WN8.getText().toString());
            map.put("WN9",WN9.getText().toString());
            map.put("WN10",WN10.getText().toString());
            map.put("WN11",WN11.getText().toString());
            map.put("WN12",WN12.getText().toString());
            map.put("WN13",WN13.getText().toString());
            map.put("WN14",WN14.getText().toString());
            map.put("WN15",WN15.getText().toString());
            map.put("WN16",WN16.getText().toString());
            map.put("WN17",WN17.getText().toString());
            map.put("WN18",WN18.getText().toString());
            map.put("WN19",WN19.getText().toString());
            map.put("WN20",WN20.getText().toString());
            map.put("PN1",PN1.getText().toString());
            map.put("PN2",PN2.getText().toString());
            map.put("PN3",PN3.getText().toString());
            map.put("PN4",PN4.getText().toString());
            map.put("PN5",PN5.getText().toString());
            map.put("PN6",PN6.getText().toString());
            map.put("PN7",PN7.getText().toString());
            map.put("PN8",PN8.getText().toString());
            map.put("PN9",PN9.getText().toString());
            map.put("PN10",PN10.getText().toString());
            map.put("PN11",PN11.getText().toString());
            map.put("PN12",PN12.getText().toString());
            map.put("PN13",PN13.getText().toString());
            map.put("PN14",PN14.getText().toString());
            map.put("PN15",PN15.getText().toString());
            map.put("PN16",PN16.getText().toString());
            map.put("PN17",PN17.getText().toString());
            map.put("PN18",PN18.getText().toString());
            map.put("PN19",PN19.getText().toString());
            map.put("PN20",PN20.getText().toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Toast.makeText(getContext(),"数据已保存",Toast.LENGTH_SHORT).show();
        SharedPreferencesUtils.getInstance().saveChartParameter(getContext(),map.toString());
        ((MainActivity)getActivity()).currentFragment = FragmentUtils.switchFragment
                (getFragmentManager(), R.id.fragment_content, ((MainActivity)getActivity()).currentFragment, SettingFragment.class, null);
    }
}
